const app = getApp();
const { AskComponent } = app._require("common/index");
AskComponent({
  properties: {
    show: {
      type: Boolean,
      value: false,
   },
   myPage: {
    type: Boolean,
    value: false,
  },
  desc:{
    type: String,
    value: "",
},
  item:{
    type: String,
    value: "",
}
  },
  data: {
    show_pwd:true
    // show:true
  }, 
  methods: {
 
      submitAction:function(e){
        this.triggerEvent('submitAction', {
            
        })
      },
      cancleAction:function(e){
        this.setData({
          "show":false
        })
      },
      registerAction(){
        
        wx.navigateTo({
          url: '/pages/login/regist/index',
        })
        // console.log("趣事多多");
      },
      loginAction(){
        this.login();
        // console.log("登录");
      },
      forgetAction(){
        wx.navigateTo({
          url:"/pages/forget-password/index"
        })
        // console.log("忘记密码");
      },
      callAction(){
        wx.makePhoneCall({
          phoneNumber: '400-8618-890',
          success:function(){
            console.log('拨打成功')
          },
          fail:function(){
            console.log('拨打失败')
          }
        })
    
        // console.log("联系客服")
      },
      onInput(e){
        
        // console.log(e)

        let value = e.detail.value;
        let key_value = e.target.dataset.key;

        // console.log(key_value);

        // console.log(e);
        if(key_value=="phone"){
          this.setData({
            "phone":value
          })
        }else{
          this.setData({
            "password":value
          })
        }
   
        // console.log(value);

      },
      onIconClick(e){
        this.setData({
          isPassText: !this.data.isPassText,
        });

      },
      async login() {

         //登录成功===>
        //  this.$emit("loginSuccess");

        let phone = this.data.phone||""
        let password = this.data.password || ""

        

         if(phone.length != 11){
           wx.showToast({
             title: '请输入正确的手机号码',
             icon:"none"
           })
           return
         }
         if(password.length ==0){
          wx.showToast({
            title: '请输入密码',
            icon:'none'
          })
          return
        }

        try{
          const response = await this.$_server("user/login", {
            data: {
                username: phone,
                password: password,
            },
             });
            let { data } = response;
            if (data.code !== 200 || (data.code === 200 && !data.data.token)) {
                // throw data;
                wx.showToast({
                  title: data.message,
                  icon:'none'
                })
                return
            }
            data.data.clearManager = true;
            data.data.clearTask = true;
            app.login(data);
            const eventChannel = this.getOpenerEventChannel();
            try{
                eventChannel.emit("sure");
            }catch{
                console.log(eventChannel);
            }

            this.setData({
              "phone":"",
              "password":""
            })

              //记录一下需要刷新页面
              wx.setStorageSync('reoladPage', {
                'home':true,
                "note":true
            })
            //登录成功===>
            this.$emit("loginSuccess");

            
        }catch(e){}

        
          

        

      
      },
      //显示密码
      clickShow(){
        // console.log(this.data.show_pwd);
        this.setData({
          "show_pwd":!this.data.show_pwd
        })
      }



  }
})
